﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using BidFunLib.DAO;
using BidFunLib.Entity;
using NHibernate;
using BidFunLib.BidWeb;
using BidFunLib.EntityHelper;

namespace BidFunLib.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class StatsBidUserByBidSaleManager
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(StatsBidUserByBidSaleManager));

        /// <summary>
        /// The dao of this manager
        /// </summary>
        private StatsBidUserByBidSaleDAO dao = new StatsBidUserByBidSaleDAO();
        
        /// <summary>
        /// Singleton instance
        /// </summary>
        private static StatsBidUserByBidSaleManager instance;

        /// <summary>
        /// Retreive singleton of this class
        /// </summary>
        public static StatsBidUserByBidSaleManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new StatsBidUserByBidSaleManager();
                }
                return StatsBidUserByBidSaleManager.instance;
            }
            set
            {
                StatsBidUserByBidSaleManager.instance = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private StatsBidUserByBidSaleManager()
        {
        }

        /// <summary>
        /// Calculate by user the aggregation of all bids performed on the sale.
        /// </summary>
        /// <param name="bidSale"></param>
        /// <param name="usersWithBid"></param>
        public Dictionary<Int32, StatsBidUserByBidSale> calculateStatsBidUserByBidSale(BidSale bidSale, Dictionary<String, BidUserWeb> usersWithBid)
        {
            log.Info("Start calculateStatsBidUserByBidSale");
            if (bidSale != null && usersWithBid != null)
            {
                // recupere de la base de données l'ensemble des données par user
                Dictionary<Int32, StatsBidUserByBidSale> statsBidsLoggers = BidLoggerManager.Instance.getMapUserLoggersForOneSale(bidSale);

                // save all data
                foreach (StatsBidUserByBidSale statsBidUserByBidSale in statsBidsLoggers.Values)
                {
                    saveOrUpdate(statsBidUserByBidSale);
                }

                return statsBidsLoggers;
            }
            return null;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="bidLogger"></param>
        /// <returns></returns>
        public StatsBidUserByBidSale saveOrUpdate(StatsBidUserByBidSale statsBidUserByBidSale)
        {
            return dao.saveOrUpdate(statsBidUserByBidSale);
        }


    }
}
